package com.manli.wechat.utils;

import com.alibaba.fastjson.JSONObject;
import com.manli.common.utils.HttpUtil;

/**
 * 仅是做请求获取微信返回数据，存取处理逻辑放自己项目
 */
public class WeChat {
    private String APP_ID;
    private String APP_SECRET;

    public WeChat(String appid,String appsecret){
        if(appid == null || appid.trim().length() == 0 || appsecret == null || appsecret.trim().length() == 0){
            throw new RuntimeException("APP_ID OR APP_SECRET IS INVALID.");
        }
        APP_ID = appid;
        APP_SECRET = appsecret;
    }

    public String getAPP_ID() {
        return APP_ID;
    }

    public String getAPP_SECRET() {
        return APP_SECRET;
    }

    public void isInited(){
        if(APP_ID == null || APP_SECRET == null){
            throw new RuntimeException("APP_ID OR APP_SECRET NOT INITED.");
        }
    }

    /**
     * 获取token
     * @return
     */
    public JSONObject getAccessToken()throws Exception{
        isInited();
        return JSONObject.parseObject(HttpUtil.httpsGet(WeChatUrl.ACCESSTOKEN_URL + "appid=" + APP_ID + "&secret=" + APP_SECRET));
    }
    /**
     * 获取JsapiTicket
     * @return
     */
    public JSONObject getJsapiTicket(String accesstoken)throws Exception{
        isInited();
        return JSONObject.parseObject(HttpUtil.httpsGet(WeChatUrl.JS_API_TICKET_URL + accesstoken + "&type=jsapi"));
    }
    /**
     * 获取微信卡券apiTicket
     * @return
     */
    public JSONObject getWxCardApiTicket(String accesstoken)throws Exception{
        isInited();
        return JSONObject.parseObject(HttpUtil.httpsGet(WeChatUrl.JS_API_TICKET_URL + accesstoken + "&type=wx_card"));
    }



}
